
using System;
using System.Collections.Generic;
using System.Linq;
using System.Drawing;
using MonoTouch.Foundation;
using MonoTouch.UIKit;
using System.IO;
using System.Threading;

namespace ComBrowser
{
	public partial class MainViewController : UIViewController
	{
		#region Constructors

		// The IntPtr and initWithCoder constructors are required for controllers that need 
		// to be able to be created from a xib rather than from managed code

		public MainViewController (IntPtr handle) : base(handle)
		{
			Initialize ();
		}

		[Export("initWithCoder:")]
		public MainViewController (NSCoder coder) : base(coder)
		{
			Initialize ();
		}

		public MainViewController () : base("MainViewController", null)
		{
			Initialize ();
		}

		void Initialize ()
		{
			
		}

		#endregion

		FlipperViewController flip = null;
		ComDomain comDomain = null;
		 
		public override void ViewDidLoad ()
		{
			//Set the toolbars as hidden
			this.NavigationController.SetNavigationBarHidden (true, false);
			this.NavigationController.SetToolbarHidden (true, false);
			
		    comDomain = ComDomain.Instance ();
			PositionDatabaseAtLastReadPosition ();
			CustomizeTitleBarAndButtons ();
			WireUpCommands ();
			base.ViewDidLoad ();
		}

		#region WireUpCommands
		private void WireUpCommands ()
		{
			btnOptions.Clicked += delegate { ShowOptions (); };
			btnNextChapter.Clicked += delegate { MoveToNextChapter (); };
			btnPreviousChapter.Clicked += delegate { MoveToPreviousChapter (); };
			btnSearch.Clicked += delegate { ShowSearch ();};
		}
		#endregion

		#region Helper: PositionDatabaseAtLastReadPosition
		void PositionDatabaseAtLastReadPosition ()
		{
			int b = Settings.ZeroBasedSelectedBookID;
			int c = Settings.ZeroBasedSelectedChapterID;
			
			if (!comDomain.BookHasChapter (b, c)) {
				comDomain.GetVeryFirstBookChapter (ref b, ref c);
				Settings.ZeroBasedSelectedBookID = b;
				Settings.ZeroBasedSelectedChapterID = c;
			}
			
		}
		#endregion

		#region Helper: ToggleMenu
		bool toolBarVisible = false;
		public void ToggleMenu ()
		{
			CreateFlipChapterPicker ();
			this.NavigationController.SetNavigationBarHidden (toolBarVisible, true);
			this.NavigationController.SetToolbarHidden (toolBarVisible, true);
			toolBarVisible = !toolBarVisible;
		}
		#endregion

		#region Helper: CreateFlipChapterPicker
		void CreateFlipChapterPicker ()
		{
			if (flip == null) {
				flip = new FlipperViewController (this);
				float left = (UIScreen.MainScreen.ApplicationFrame.Width - flip.View.Frame.Width) / 2;
				float top = (UIScreen.MainScreen.ApplicationFrame.Height - flip.View.Frame.Height) / 2;
				flip.View.Frame = new System.Drawing.RectangleF (left, top, flip.View.Frame.Width, flip.View.Frame.Height);
				flip.View.Hidden = true;
				this.View.AddSubview (flip.View);
			}
		}
		#endregion

		#region Helper: CustomizeTitleBar
		void CustomizeTitleBarAndButtons ()
		{
			//Chapter button
			UIBarButtonItem btnVerse = new UIBarButtonItem ();
			btnVerse.Title = "Chapter";
			btnVerse.Clicked += delegate {
				this.ToggleMenu ();
				flip.View.Hidden = false;
			};
			this.NavigationItem.SetRightBarButtonItem (btnVerse, false);

			
		
  			btnNextChapter.Image = UIImage.FromFileUncached (Path.Combine (NSBundle.MainBundle.BundlePath, "Images/Right.png"));
			btnPreviousChapter.Image = UIImage.FromFileUncached (Path.Combine (NSBundle.MainBundle.BundlePath, "Images/Left.png"));
			btnSearch.Image = UIImage.FromFileUncached (Path.Combine (NSBundle.MainBundle.BundlePath, "Images/Search.png"));
			btnNote.Image = UIImage.FromFileUncached (Path.Combine (NSBundle.MainBundle.BundlePath, "Images/Notes.png"));
			btnOptions.Image = UIImage.FromFileUncached (Path.Combine (NSBundle.MainBundle.BundlePath, "Images/Options.png"));

			
		}
		#endregion

		#region Event: ViewWillAppear (bool animated)
		public override void ViewWillAppear (bool animated)
		{
			WebView.Delegate = new WebMainViewDelegate (this);
			ShowChapter (Settings.ZeroBasedSelectedBookID, Settings.ZeroBasedSelectedChapterID);
		}
		#endregion

		#region Helper: ShowChapter
		void ShowChapter (int ZeroBasedBookID, int ZeroBasedChapterID)
		{
			
			this.NavigationItem.Title = "Loading...";
			NSUrl path = new NSUrl (NSBundle.MainBundle.BundlePath);
			
			string clearEffects = @"
				<script>
				document.documentElement.style.webkitTapHighlightColor = 'rgba(0,0,0,0)';
				document.documentElement.style.webkitTouchCallout = 'none';
				
				function ClickGeneralArea(){{
				if(event.srcElement.tagName!='A')
				   window.location = '#command:ShowMenu';
				}}
				</script>";
			
			string html = string.Format (@"{0}{1}<body>{2}</body>", StyleSheet.CSS, clearEffects, ComDomain.Instance ().GetCommentaryHTML (ZeroBasedBookID, ZeroBasedChapterID));
			
			WebView.LoadHtmlString (html, path);
			this.NavigationItem.Title = comDomain.GetTitle (Settings.ZeroBasedSelectedBookID, Settings.ZeroBasedSelectedChapterID);
			
		}
		#endregion

		#region Helper: MoveToNextChapter
		private void MoveToNextChapter ()
		{
			int zeroBasedSelectedBookID = Settings.ZeroBasedSelectedBookID;
			int zeroBasedChapterID = Settings.ZeroBasedSelectedChapterID;
			
			comDomain.GetNextChapterIDs (ref zeroBasedSelectedBookID, ref zeroBasedChapterID);
			Settings.ZeroBasedSelectedBookID = zeroBasedSelectedBookID;
			Settings.ZeroBasedSelectedChapterID = zeroBasedChapterID;
			
			ShowChapter (zeroBasedSelectedBookID, zeroBasedChapterID);
		}
		#endregion

		#region Helper: MoveToPreviousChapter
		private void MoveToPreviousChapter ()
		{
			int zeroBasedSelectedBookID = Settings.ZeroBasedSelectedBookID;
			int zeroBasedChapterID = Settings.ZeroBasedSelectedChapterID;
			
			comDomain.GetPreviousChapterIDs (ref zeroBasedSelectedBookID, ref zeroBasedChapterID);
			Settings.ZeroBasedSelectedBookID = zeroBasedSelectedBookID;
			Settings.ZeroBasedSelectedChapterID = zeroBasedChapterID;
			
			ShowChapter (zeroBasedSelectedBookID, zeroBasedChapterID);
		}
		#endregion

		#region Event: ChapterPicked
		public void ChapterPicked (int zeroBasedSelectedBookID, int zeroBasedChapterID)
		{
			Settings.ZeroBasedSelectedBookID = zeroBasedSelectedBookID;
			Settings.ZeroBasedSelectedChapterID = zeroBasedChapterID;
			
			ShowChapter (zeroBasedSelectedBookID, zeroBasedChapterID);
		}
		#endregion

		#region Helper: ShowOptions()
		OptionsViewController optionViewController = null;
		private void ShowOptions ()
		{
			if (optionViewController == null)
				optionViewController = new OptionsViewController (this);
			
			ToggleMenu ();
			this.View.AddSubview (optionViewController.View);
		}

		#endregion

		#region Helper: HideOptions()
		public void HideOptions ()
		{
			optionViewController.View.RemoveFromSuperview ();
			optionViewController = null;
			ShowChapter (Settings.ZeroBasedSelectedBookID, Settings.ZeroBasedSelectedChapterID);
		}
		#endregion
		
		#region Helper: ShowSearch()
		SearchViewController searchViewController=null;
		public void ShowSearch ()
		{
			if (searchViewController == null)
				searchViewController = new SearchViewController (this);
	
			ToggleMenu ();
			this.View.AddSubview (searchViewController.View); 
		}
		#endregion

		#region Helper: HideSearch()
		public void HideSearch ()
		{
			View.SendSubviewToBack (searchViewController.View);
		}
		#endregion
		
	}
}
